package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.service.ChartComparisonServiceAsync;
import ru.spb.osll.myfit.client.widget.ChartComparator;
import ru.spb.osll.myfit.client.widget.IStyleConstants;
import ru.spb.osll.myfit.common.chart.ChartData;

import java.util.List;

public class ChartComparisonView extends AbstractView {
  private List<String> m_ids;
  private final Widget m_titleLabel;
  private final VerticalPanel m_contentPanel = new VerticalPanel();
  private ChartComparisonServiceAsync m_chartComparisonService;

  public ChartComparisonView(List<String> ids, ChartComparisonServiceAsync chartsService) {
    m_ids = ids;
    m_titleLabel = createTitlePanel();

    m_chartComparisonService = chartsService;

    createRootPanel();
  }

  private Widget createTitlePanel() {
    HorizontalPanel panel = new HorizontalPanel();
    panel.setStyleName(IStyleConstants.TITLE_PANEL_STYLE_NAME);
    Label prefix = new Label("Chart: ");
    prefix.addStyleName(IStyleConstants.PAGE_TITLE_LABEL_STYLE_NAME);
    panel.add(prefix);
    panel.setCellWidth(prefix, "auto");

    StringBuilder idsToString = new StringBuilder("");
    idsToString.append(m_ids.get(0));
    for (int i = 1; i < m_ids.size(); ++i) {
      idsToString.append(" vs. " + m_ids.get(i));
    }

    HTML title = new HTML(idsToString.toString());
    title.addStyleName(IStyleConstants.PAGE_TITLE_STYLE_NAME);
    panel.add(title);
    panel.setCellWidth(title, "100%");

    return panel;
  }

  @Override
  protected Widget createContentPanel() {
    return m_contentPanel;
  }

  @Override
  protected Widget createEditPanel() {
    VerticalPanel editPanel = new VerticalPanel();
    editPanel.setStyleName(IStyleConstants.MYFIT_VIEW_TOOL_PANEL_STYLE_NAME);
    editPanel.add(m_titleLabel);
    editPanel.add(createButtonPanel());
    return editPanel;
  }

  private Widget createButtonPanel() {
    Panel buttonPanel = new HorizontalPanel();

    return buttonPanel;
  }

  public void display(List<ChartData> chartDataList) {
    m_contentPanel.add(new ChartComparator(m_ids, chartDataList, m_chartComparisonService));
  }


}
